package wyq.gy;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.SaveCallback;
import java.util.ArrayList;
import java.util.List;
import wyq.gy.Bean.workerInfo;
import wyq.gy.Tool.util;
import wyq.gy.adapter.workerListAdapter;
import wyq.gy.fragment.CountDonwDlgFragment;
import wyq.gy.fragment.InfoDialogFragment;
import wyq.gy.lib.TimeLineView;

import static android.view.View.VISIBLE;

public class OrderDetailActivity extends AppCompatActivity {
    private List<String> data;
    private TextView content;
    private TextView category;
    private TextView Mystatus;
    private TextView handleDate;
    private TextView publicTime;
    private TextView address;
    private Button finishWorkBtn;
    private TimeLineView mView2;
    private String m_orderId;

    private List<workerInfo> workerList;
    private workerListAdapter mWorkerListAdapter;
    CountDonwDlgFragment dialog = new CountDonwDlgFragment();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setTitle("订单详情");
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        m_orderId = getIntent().getStringExtra("orderId");
        InitView();
        GetOrderDetail();
    }

    private void InitView(){
        mView2 = (TimeLineView) findViewById(R.id.timeLineView2);
        mView2.setVisibility(View.INVISIBLE);
        content = (TextView) findViewById(R.id.contentorder);
        category = (TextView) findViewById(R.id.category);
        Mystatus = (TextView) findViewById(R.id.status);
        publicTime= (TextView) findViewById(R.id.public_time);
        handleDate = (TextView) findViewById(R.id.handleDate);
        address = (TextView) findViewById(R.id.address);
        finishWorkBtn = (Button) findViewById(R.id.finish);
        finishWorkBtn.setEnabled(false);
        finishWorkBtn.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.gray));
        finishWorkBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EnsureWorkDone();
            }
        });
        data = new ArrayList<>();
        data.add("信息发布");
        data.add("工人接单");
        data.add("雇主确认");
        data.add("工人完成");

       // ListView wokerListView = (ListView)findViewById(R.id.wokerListLV);
        workerList= new ArrayList<workerInfo>();
        mWorkerListAdapter = new workerListAdapter(this);
        mWorkerListAdapter.setData(workerList);
       // wokerListView.setAdapter(mWorkerListAdapter);

//        Button ensureBtn = (Button)findViewById(R.id.ensure_btn);
//        ensureBtn.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                if (event.getAction() == MotionEvent.ACTION_DOWN) {
//                    Bundle bundle = new Bundle();
//                    bundle.putString("orderId", m_orderId);
//                    dialog.setArguments(bundle);
//                    dialog.show(getFragmentManager(), "CountDonwDlgFragment");
//                }
//                if (event.getAction() == MotionEvent.ACTION_UP) {
//                    dialog.dismiss();
//                }
//                return true;
//            }
//        });
    }

    private void GetOrderDetail(){  //从后台获取状态
        AVQuery<AVObject> query = new AVQuery<>("MyOrderList");
        query.getInBackground(m_orderId, new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                ShowOrderDetial(avObject);
                AVQuery<AVObject> query = new AVQuery<>("RobMyOrderList");
                query.whereEqualTo("OrderNumber",avObject.getObjectId());
                query.whereEqualTo("State","3");
                query.findInBackground(new FindCallback<AVObject>(){
                    @Override
                    public void done(List<AVObject> list, AVException e) {
                        workerList.clear();
                        for(int i = 0;i < list.size(); i++){
                            workerInfo info = new workerInfo();
                            info.workerName = list.get(i).getString("RoberName");
                            info.phone = list.get(i).getString("robWokerPhone");
                            info.birthday = list.get(i).getString("robBirth");
                            info.star = list.get(i).getString("star");
                            workerList.add(info);
                        }
                        mWorkerListAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    private void ShowOrderDetial(AVObject avObject ){
        mView2.setVisibility(VISIBLE);
        content.setText(avObject.getString("ServiceContent"));
        category.setText(avObject.getString("RepairCategory"));
        handleDate.setText(avObject.getString("HandleTime"));
        address.setText(avObject.getString("RealAddress"));
        publicTime.setText(avObject.getString("PublicTime"));

        int state = Integer.parseInt(avObject.getString("Status"));
        Mystatus.setText(util.GetStatusText(avObject.getString("Status")));
        mView2.builder().pointStrings(data, state).load();
        if (state == 4) {
            finishWorkBtn.setEnabled(false);
            finishWorkBtn.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.gray));
        }else if(state == 1){
            Button modify_btn = (Button)findViewById(R.id.modify_btn);
            modify_btn.setEnabled(true);
            modify_btn.setVisibility(VISIBLE);
            modify_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("orderId",m_orderId);
                    intent.setClass(OrderDetailActivity.this, ReleaseOrderActivity.class);
                    startActivity(intent);
                }
            });
        }
        else if(state == 2) {
            mView2.builder().pointStrings(data, 2).load();
            finishWorkBtn.setEnabled(true);
            finishWorkBtn.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.hotgrade4));
        }
//        else if(state == 3) {
//            mView2.builder().pointStrings(data, 3).load();
//            finishWorkBtn.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.hotgrade4));
//        }
    }


    private void EnsureWorkDone(){
        AVObject todo = AVObject.createWithoutData("MyOrderList", m_orderId);
        todo.put("Status","3");
        todo.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    Toast.makeText(OrderDetailActivity.this, "提交完成", Toast.LENGTH_LONG).show();
                    mView2.builder().pointStrings(data, 3).load();
                    finishWorkBtn.setEnabled(false);
                    finishWorkBtn.setBackgroundColor(ContextCompat.getColor(getApplicationContext(),R.color.gray));
                } else {
                    Toast.makeText(OrderDetailActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
